package br.com.androidblog;

import java.io.IOException;

import br.com.androidblog.database.DataBaseConnector;
import br.com.androidblog.rss.RSSReader;

import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class Splash extends Activity {

	public final boolean isInternetOn() {

		ConnectivityManager connec = (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);

		// ARE WE CONNECTED TO THE NET

		if (connec.getNetworkInfo(0).getState() == State.CONNECTED
				||

				connec.getNetworkInfo(0).getState() == State.CONNECTING
				||

				connec.getNetworkInfo(1).getState() == State.CONNECTING
				||

				connec.getNetworkInfo(1).getState() == State.CONNECTED) {


			return true;

		} else if (connec.getNetworkInfo(0).getState() == State.DISCONNECTED
				|| connec.getNetworkInfo(1).getState() == State.DISCONNECTED) {

			// System.out.println("Not Connected");

			return false;

		}

		return false;

	}

	/** Called when the activity is first created. */
	private final int SPLASH_DISPLAY_LENGHT = 1000;

	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.splash);

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				/* Create an Intent that will start the Menu-Activity. */

				DataBaseConnector d = new DataBaseConnector(
						getApplicationContext());
				try {
					d.createDataBase();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (isInternetOn() == true){
					try {
						
						RSSReader r = new RSSReader(getApplicationContext());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					Log.i("androidblog","Sem acesso a internet  ");
				}

				Intent mainIntent = new Intent(Splash.this, ListaBlog.class);
				Splash.this.startActivity(mainIntent);
				Splash.this.finish();
			}
		}, SPLASH_DISPLAY_LENGHT);
	}
}
